<?php

class Usefull {

    /**
     *
     * convertChar - Convertit la chaine
     * de caractère reçu.
     */
    public function convertChar($texte) {
        global $siteTheme;
        $texte = preg_replace('#\[b\](.+)\[/b\]#isU', '<strong>$1</strong>', $texte);
        $texte = preg_replace('#\[i\](.+)\[/i\]#isU', '<em>$1</em>', $texte);
        $texte = preg_replace('#\[u\](.+)\[/u\]#isU', '<u>$1</u>', $texte);
        $texte = preg_replace('#\[s\](.+)\[/s\]#isU', '<s>$1</s>', $texte);
        $texte = preg_replace('#\[cmd\](.+)\[/cmd\]#isU', '<b class=\"cmd\">$1</b>', $texte);
        $texte = preg_replace('#\[lien\](.+)\[/lien\]#isU', '<u><b>$1</b></u>', $texte);
        $texte = preg_replace('#\[img\](.+)\[/img\]#isU', '<img src="./$1" alt="image" />', $texte);
        $texte = preg_replace('#\[color=(red|green|blue|yellow|purple|olive|grey|cyan|orange|magenta|pink)\](.+)\[/color\]#isU', '<span style="color:$1">$2</span>', $texte);
        $texte = preg_replace('#\[url=(.+)\](.+)\[/url\]#isU', '<a href="$1" style="color:navy;font-weight:bold;" target="_BLANK" title="Voir le lien $1">$2</a>', $texte);
        return $texte;
    }

    /**
     *
     * convertDate - Convertit la date du format
     * americain a celui d'européen. Et remplace
     * les - par des /
     */
    public static function convertDate($date) {
        $date_array = explode("-", $date);
        $day = $date_array[2];
        $month = $date_array[1];
        $year = $date_array[0];

        switch ($month) {
            case '01':
                $month = "Janvier";
                break;
            case '02':
                $month = "F&eacute;vrier";
                break;
            case '03':
                $month = "Mars";
                break;
            case '04':
                $month = "Avril";
                break;
            case '05':
                $month = "Mai";
                break;
            case '06':
                $month = "Juin";
                break;
            case '07':
                $month = "Juillet";
                break;
            case '08':
                $month = "Ao&ucirc;t";
                break;
            case '09':
                $month = "Septembre";
                break;
            case '10':
                $month = "Octobre";
                break;
            case '11':
                $month = "Novembre";
                break;
            case '12':
                $month = "D&eacute;cembre";
                break;
            default:
                break;
        }
        return $day . " " . $month . " " . $year;
        //return $date_array[2] . "/" . $date_array[1] . "/" . $date_array[0];
    }

    /**
     * displayPopUp - Affiche un PopUp qui varie
     * en fonction du $type et qui affiche le message
     * $message.
     */
    public static function displayPopUp($type="info", $message) {
?>
        <div id="<?php echo "box" . $type; ?>" onclick="closePopUp('<?php echo "box" . $type; ?>')" class="<?php echo $type . "-box rounded"; ?>" style="text-align: left;" title="Fermer la fen&ecirc;tre">
    <?php echo $message; ?>
    </div>
<?php
    }

    /**
     *
     * convertHour - Convertit l'heure d'une 
     * maniüre plus habituelle. 
     */
    public static function convertHour($hour) {
        $hour_array = explode(":", $hour);

        return $hour_array[0] . "h" . $hour_array[1];
    }

    /**
     * displayLastComment - Affiche le 
     * commentaire le plus récent.
     */
    public function displayLastComment() {

        $PDO = getDbConnection();

        $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_COMMENT . " ORDER BY id DESC ");
        $req->execute();
        $comment = $req->fetch();

        if ($comment == null) {
            echo "Aucun commentaire n'a encore &eacute;t&eacute; post&eacute;.";
        } else {
            foreach ($comment as $key => $value) {
                switch ($key) {
                    case 'id':
                        $id = $value;
                        break;
                    case 'id_news':
                        $id_news = $value;
                        break;
                    case 'author':
                        $author = ucfirst($value);
                        break;
                    case 'comment':
                        $content = $this->convertChar(ucfirst($value));
                        break;
                    case 'date':
                        $date = $this->convertDate($value);
                        break;
                    case 'hour':
                        $hour = $this->convertHour($value);
                        break;
                    default:

                        break;
                }
            }
            echo "<p class=\"px10\">Post&eacute; par <b><a href=\"./index.php?page=user&user=" . $author . "\" title=\"Voir le profil de " . $author . "\">" . $author . "</a></b>, le " . $date . " &agrave; " . $hour . "</p>";
            echo $content . "<br />";
            echo "<i class=\"px10 right\"><a href=\"./index.php?page=comment&news=" . $id_news . "\">Lire la news</a></i>";
        }
    }

    /**
     * displayLastRegister - Affiche la dernière
     * inscription.
     */
    public function displayLastRegister() {

        $PDO = getDbConnection();

        $req = $PDO->prepare("SELECT * FROM  " . DB_NAME . "." . TB_UTILISATEURS . " ORDER BY id_user DESC ");
        $req->execute();
        $user = $req->fetch();

        if ($user == null) {
            echo "Aucun utilisateur ne c'est encore inscrit.";
        } else {
            foreach ($user as $key => $value) {
                switch ($key) {
                    case 'login':
                        $login = ucfirst($value);
                        break;
                    case 'nom':
                        $nom = $this->convertChar(ucfirst($value));
                        break;
                    case 'prenom':
                        $prenom = $this->convertChar(ucfirst($value));
                        break;
                    case 'email':
                        $email = $value;
                        break;
                    case 'sexe':
                        $sexe = $value;
                        break;
                    case 'pays':
                        $pays = $value;
                        break;
                    case 'date_naissance':
                        $dateNaissance = $this->convertDate($value);
                        break;
                    default:

                        break;
                }
            }
            echo "<p class=\"px10\"><b><a href=\"./index.php?page=user&user=" . $login . "\" title=\"Voir le profil de " . $login . "\">" . $login . "</a></b> est le dernier utilisateur inscrit.</p>";
            if ($sexe == 0) {
                echo "Il se nomme " . $prenom . "&nbsp;" . $nom . ", et est n&eacute; le " . $dateNaissance . ". Il habite en " . $pays . ". <br /><br /> Email : <a href=\"mailto:" . $email . "\">" . $email . "</a>";
            } else {
                echo "Elle se nomme " . $prenom . "&nbsp;" . $nom . ", et est n&eacute;e le " . $dateNaissance . ". Elle habite en " . $pays . ". <br /><br /> Email : <a href=\"mailto:" . $email . "\">" . $email . "</a>";
            }
        }
    }

}
?>
